//
//  UIViewController+AddTitleView.m
//  VLive2
//
//  Created by allenapple on 12-4-8.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "UIViewController+AddTitleView.h"

@implementation UIViewController (AddTitleView)
/*****
 set  VIewControllerTitle
 *****/
-(void)setNavBarTitle:(NSString *)_titled orImaged:(NSString *)imageName{

    
    NSLog(@"VIewControllerTitle");
    
    if (_titled) {
        CGRect frame = CGRectMake(0, 0, 200, 44); 
        UILabel *label = [[UILabel alloc] initWithFrame:frame]; 
        label.backgroundColor = [UIColor clearColor]; 
        label.font = [UIFont boldSystemFontOfSize:20.0]; 
        //    label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5]; 
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor whiteColor]; 
//         label.textColor = [UIColor colorWithRed:0.663 green:0 blue:0.267 alpha:1.0]; 
        label.text=_titled;
        self.navigationItem.titleView = label;
        [label release];
    }else{
        
        UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake(105, 2, 110, 40)];
        imageview.image=[UIImage imageNamed:imageName];
        self.navigationItem.titleView=imageview;
        [imageview release];
    }


}
/*****
set  leftBarButton
 *****/
-(void)setleftBarButtonItem:(NSString *)_titled orImaged:(NSString *)imageName Event:(SEL)_event{

     NSLog(@"setleftBarButtonItem");
//    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 92/2, 88/2)];
//    btn.titleLabel.font=[UIFont systemFontOfSize:14];
//    if (_titled) {
//        [btn setTitle: _titled forState:UIControlStateNormal];
//    }
//    if (imageName) {
//        [btn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
//    }
//    [btn addTarget:self action:_event forControlEvents:UIControlEventTouchUpInside];
//    
//    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
//    [btn release];
//    self.navigationItem.leftBarButtonItem=item;
//    [item release];
    
    
    UIButton * backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0, 92/2, 88/2);
    if ([_titled length] && _titled != nil) {
        [backButton setTitle: _titled forState:UIControlStateNormal];
        [backButton.titleLabel setFont:[UIFont boldSystemFontOfSize:14.0]];
        [backButton setTitleColor:[UIColor colorWithRed:233.0/255.0 green:220.0/255.0 blue:207.0/255.0 alpha:1.0] forState:UIControlStateNormal];
        [backButton setTitleEdgeInsets:UIEdgeInsetsMake(-1, -55, 0, 0)];
    }else{
        NSLog(@"_titled is nil or something else");
    }
    if ([imageName length] && imageName != nil) {
        [backButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    }else{
        NSLog(@"imagenamed==%@",imageName);
    }
    [backButton addTarget:self action:_event forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *backNavigationItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    if(([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0?20:0)){
        UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                           initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                           target:nil action:nil];
        negativeSpacer.width = -10;
        self.navigationItem.leftBarButtonItems = @[negativeSpacer, backNavigationItem];
    }else{
        self.navigationItem.leftBarButtonItem = backNavigationItem;
    }
}

-(void)setleftBarButtonItem:(NSString *)imageName   Event:(SEL)_event{
    
//    UIView *BarView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 44, 44)];
//    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake((44-22.5)/2-8, (44-22.5)/2, 22.5, 22.5)];
//    //    btn.backgroundColor=[UIColor redColor];
//    btn.titleLabel.font=[UIFont systemFontOfSize:14];
//    if (imageName) {
//        [btn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
//    }
//    [btn addTarget:self action:_event forControlEvents:UIControlEventTouchUpInside];
//    
//    [btn setHitTestEdgeInsets:UIEdgeInsetsMake(-11, -11,-11,-11)];//不能小数
//    [BarView addSubview:btn];
//    [btn release];
//    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:BarView];
//    [BarView release];
//    self.navigationItem.leftBarButtonItem=item;
//    [item release];
    
    UIButton * backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0, 92/2, 88/2);
    
    if ([imageName length] && imageName != nil) {
        [backButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    }else{
        NSLog(@"imagenamed==%@",imageName);
    }
    [backButton addTarget:self action:_event forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *backNavigationItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    if(([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0?20:0)){
        UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                           initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                           target:nil action:nil];
        negativeSpacer.width = -10;
        self.navigationItem.leftBarButtonItems = @[negativeSpacer, backNavigationItem];
    }else{
        self.navigationItem.leftBarButtonItem = backNavigationItem;
    }
}


/*****
 set  rightBarButton
 *****/
-(void)setrightBarButtonItem:(NSString *)_titled orImaged:(NSString *)imageName Event:(SEL)_event{

//    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 92/2, 88/2)];
//    btn.titleLabel.font=[UIFont systemFontOfSize:14];
//    if (_titled) {
//        [btn setTitle:_titled forState:UIControlStateNormal];
//    }
//    if (imageName) {
//        [btn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
//    }
//    [btn addTarget:self action:_event forControlEvents:UIControlEventTouchUpInside];
//    
//    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
//    [btn release];
//    self.navigationItem.rightBarButtonItem=item;
//    [item release];
    UIButton * backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0, 92/2, 88/2);
    if ([_titled length] && _titled != nil) {
        [backButton setTitle: _titled forState:UIControlStateNormal];
        [backButton.titleLabel setFont:[UIFont boldSystemFontOfSize:14.0]];
        [backButton setTitleColor:[UIColor colorWithRed:233.0/255.0 green:220.0/255.0 blue:207.0/255.0 alpha:1.0] forState:UIControlStateNormal];
        [backButton setTitleEdgeInsets:UIEdgeInsetsMake(-1, -55, 0, 0)];
    }else{
        NSLog(@"_titled is nil or something else");
    }
    if ([imageName length] && imageName != nil) {
        [backButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    }else{
        NSLog(@"imagenamed==%@",imageName);
    }
    [backButton addTarget:self action:_event forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *backNavigationItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    if(([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0?20:0)){
        UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                           initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                           target:nil action:nil];
        negativeSpacer.width = -10;
        self.navigationItem.rightBarButtonItems = @[negativeSpacer, backNavigationItem];
    }else{
        self.navigationItem.leftBarButtonItem = backNavigationItem;
    }
}

@end
